package co.gov.medellin.ssti.sub.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

import javax.naming.NamingException;

import co.gov.medellin.ssti.sub.common.ConexionFactory;
import co.gov.medellin.ssti.sub.model.dao.DAOFactory;
import co.gov.medellin.ssti.sub.model.dao.ISubprogramaSedeDAO;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.vo.SubprogramaSedeVO;

public class SubprogramaSedeService {
	/**
	 * 
	 * @param criteria
	 * @return
	 * @throws DAOException
	 */
	public Collection<SubprogramaSedeVO> findSubprogramaSedeByCriteria(
			SubprogramaSedeVO criteria) throws DAOException {
		ISubprogramaSedeDAO subprogramaSedeDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			subprogramaSedeDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getSubprogramaSedeDAO(con);
			return subprogramaSedeDAO.findSubprogramaSedeByCriteria(criteria);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (subprogramaSedeDAO != null) {
				if (!subprogramaSedeDAO.isConnClosed()) {
					subprogramaSedeDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param codeSubprogramaSede
	 * @return
	 * @throws DAOException
	 */
	public Collection<SubprogramaSedeVO> findSubprogramaSedeByCode(
			Long codeSubprogramaSede) throws DAOException {
		ISubprogramaSedeDAO subprogramaSedeDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			subprogramaSedeDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getSubprogramaSedeDAO(con);
			return subprogramaSedeDAO
					.findSubprogramaSedeByCode(codeSubprogramaSede);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (subprogramaSedeDAO != null) {
				if (!subprogramaSedeDAO.isConnClosed()) {
					subprogramaSedeDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param codeSubprogramaSede
	 * @param codeUser
	 * @param param
	 * @throws DAOException
	 */
	public void insertUsuarioSede(Long codeSubprogramaSede, Long codeUser,
			String param[]) throws DAOException {
		ISubprogramaSedeDAO subprogramaSedeDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			subprogramaSedeDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getSubprogramaSedeDAO(con);
			subprogramaSedeDAO.insertUsuarioSede(codeSubprogramaSede, codeUser,
					param);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (subprogramaSedeDAO != null) {
				if (!subprogramaSedeDAO.isConnClosed()) {
					subprogramaSedeDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param codeSede
	 * @param codeSubprograma
	 * @param desde
	 * @param hasta
	 * @param cantidad
	 * @param param
	 * @return
	 * @throws DAOException
	 */
	public String[] insertSubprogramaSede(Long codeSede, Long codeSubprograma,
			String desde, String hasta, String cantidad, String param[])
			throws DAOException {
		ISubprogramaSedeDAO subprogramaSedeDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			subprogramaSedeDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getSubprogramaSedeDAO(con);
			return subprogramaSedeDAO.insertSubprogramaSede(codeSede,
					codeSubprograma, desde, hasta, cantidad, param);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (subprogramaSedeDAO != null) {
				if (!subprogramaSedeDAO.isConnClosed()) {
					subprogramaSedeDAO.close();
				}
			}
		}
	}

	/**
	 * 
	 * @param codeSubprogramaSede
	 * @param parArr
	 * @return
	 * @throws DAOException
	 */
	public boolean actualizarSubproSede(Long codeSubprogramaSede,
			String parArr[]) throws DAOException {
		ISubprogramaSedeDAO subprogramaSedeDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			subprogramaSedeDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE)
					.getSubprogramaSedeDAO(con);
			return subprogramaSedeDAO.actualizarSubproSede(codeSubprogramaSede,
					parArr);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (subprogramaSedeDAO != null) {
				if (!subprogramaSedeDAO.isConnClosed()) {
					subprogramaSedeDAO.close();
				}
			}
		}
	}
}
